
#include "stdafx.h"
//#include "Resource.h"
#include "SkinSilderCtrl.h"
#include "MemDC.h"

//////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CSkinSliderCtrl, CSliderCtrl)

BEGIN_MESSAGE_MAP(CSkinSliderCtrl, CSliderCtrl)
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_GETDLGCODE()
	ON_WM_KEYDOWN()
	ON_WM_CREATE()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////////

CSkinSliderCtrl::CSkinSliderCtrl()
{
	m_nPos = m_nMin = 0;
	m_nMax = 100;
	m_nPage = 20;
	m_crBackColor = RGB(229,242,255);

	m_bLButtonDown = FALSE;
	m_bVertical = FALSE;
}

CSkinSliderCtrl::~CSkinSliderCtrl()
{
}

BOOL CSkinSliderCtrl::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}

void CSkinSliderCtrl::OnPaint() 
{
	CPaintDC dc(this);
	
	CRect rcClient;
	GetClientRect(&rcClient);

	CMemDC MemDC(&dc, &rcClient);

	//MemDC.FillSolidRect(&rcClient, m_crBackColor);

	if (m_bVertical == TRUE)
	{
		m_ImageBack.Draw(MemDC.m_hDC, (rcClient.Width()-m_ImageBack.GetWidth())/2, rcClient.top);
		if (m_ImageBack2.IsValid())
			m_ImageBack2.Draw(MemDC.m_hDC, (rcClient.Width()-m_ImageBack2.GetWidth())/2, m_ImageBack2.GetWidth(),rcClient.top + Pos2Pixel(m_nPos), m_ImageBack2.GetHeight());
	}
	else
	{
		m_ImageBack.Draw(MemDC.m_hDC, rcClient.left, (rcClient.Height()-m_ImageBack.GetHeight())/2);
		if (m_ImageBack2.IsValid())
			m_ImageBack2.Draw(MemDC.m_hDC, rcClient.left, (rcClient.Height()-m_ImageBack2.GetHeight())/2, Pos2Pixel(m_nPos), m_ImageBack2.GetHeight());
	}

	if (m_bVertical == TRUE)
	{
		m_ImageThumb.Draw(MemDC.m_hDC, (rcClient.Width()-m_ImageThumb.GetWidth())/2, Pos2Pixel(m_nPos)-m_ImageThumb.GetHeight()/2);
	}
	else
	{
		m_ImageThumb.Draw(MemDC.m_hDC, Pos2Pixel(m_nPos)-m_ImageThumb.GetWidth()/2, (rcClient.Height()-m_ImageThumb.GetHeight())/2);
	}
}

int CSkinSliderCtrl::Pos2Pixel( int nPos )
{
	CRect rcClient;
	GetClientRect(&rcClient);
	if (m_bVertical)
		return (rcClient.Height()-m_ImageThumb.GetHeight()) * (m_nPos-m_nMin)/(m_nMax-m_nMin)+m_ImageThumb.GetHeight()/2;
	else
		return (rcClient.Width()-m_ImageThumb.GetWidth()) * (m_nPos-m_nMin)/(m_nMax-m_nMin) + m_ImageThumb.GetWidth()/2;
}

int CSkinSliderCtrl::Pixel2Pos( int nPixel )
{
	CRect rcClient;
	GetClientRect(&rcClient);

	if( m_bVertical ) {

		return (int)(
			m_nMin +
			(double)( nPixel - m_ImageThumb.GetHeight()/2) /
			( rcClient.Height()- m_ImageThumb.GetHeight() ) *
			( m_nMax - m_nMin )
			);

	} else {

		return (int)(
			m_nMin +
			(double)( nPixel - m_ImageThumb.GetWidth()/2 ) /
			( rcClient.Width() -m_ImageThumb.GetWidth() ) *
			( m_nMax - m_nMin )
			);
	}
}

void CSkinSliderCtrl::SetRangeMax(int nMax, BOOL bRedraw)
{
	m_nMax = nMax;
	if( bRedraw )
		Invalidate(FALSE);
}

void CSkinSliderCtrl::SetRangeMin(int nMin, BOOL bRedraw)
{
	m_nMin = nMin;
	if( bRedraw )
		Invalidate(FALSE);
}

void CSkinSliderCtrl::SetRange(int nMin, int nMax, BOOL bRedraw)
{
	SetRangeMin( nMin, FALSE );
	SetRangeMax( nMax, bRedraw );
}

void CSkinSliderCtrl::SetPos(int nPos)
{
	m_nPos = nPos;

	if( m_nPos > m_nMax )
		m_nPos = m_nMax;
	if( m_nPos < m_nMin )
		m_nPos = m_nMin;

	Invalidate(FALSE);
}

void CSkinSliderCtrl::SetBackImage(LPCTSTR pszFileName, DWORD imagetype)
{
	m_ImageBack.Load(pszFileName,imagetype);
	m_ImageBack.SetTransIndex(0);
	m_ImageBack.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	SetWindowPos(NULL, 0,0, m_ImageBack.GetWidth(), !m_ImageThumb.IsValid()?0:m_ImageThumb.GetHeight(), SWP_NOZORDER|SWP_NOMOVE);
}

void CSkinSliderCtrl::SetBackImage(HRSRC hRes, DWORD imagetype, HMODULE hModule)
{
	m_ImageBack.LoadResource(hRes, imagetype, hModule);
	m_ImageBack.SetTransIndex(0);
	m_ImageBack.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	SetWindowPos(NULL, 0,0, m_ImageBack.GetWidth(), m_ImageBack.GetHeight(),SWP_NOZORDER | SWP_NOMOVE);
	SetWindowPos(NULL, 0,0, m_ImageBack.GetWidth(), !m_ImageThumb.IsValid()?0:m_ImageThumb.GetHeight(), SWP_NOZORDER|SWP_NOMOVE);
}

void CSkinSliderCtrl::SetBack2Image(LPCTSTR pszFileName, DWORD imagetype)
{
	m_ImageBack2.Load(pszFileName,imagetype);
	m_ImageBack2.SetTransIndex(0);
	m_ImageBack2.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
}

void CSkinSliderCtrl::SetBack2Image(HRSRC hRes, DWORD imagetype, HMODULE hModule)
{
	m_ImageBack2.LoadResource(hRes, imagetype, hModule);
	m_ImageBack2.SetTransIndex(0);
	m_ImageBack2.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
}

void CSkinSliderCtrl::SetThumbImage(LPCTSTR pszFileName, DWORD imagetype)
{
	m_ImageThumb.Load(pszFileName,imagetype);
	m_ImageThumb.SetTransIndex(0);
	m_ImageThumb.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	SetWindowPos(NULL, 0,0, !m_ImageBack.IsValid()?0:m_ImageBack.GetWidth(), m_ImageThumb.GetHeight(), SWP_NOZORDER|SWP_NOMOVE);
}


void CSkinSliderCtrl::SetThumbImage(HRSRC hRes, DWORD imagetype, HMODULE hModule)
{
	m_ImageThumb.LoadResource(hRes, imagetype, hModule);
	m_ImageThumb.SetTransIndex(0);
	m_ImageThumb.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	SetWindowPos(NULL, 0,0, !m_ImageBack.IsValid()?0:m_ImageBack.GetWidth(), m_ImageThumb.GetHeight(), SWP_NOZORDER|SWP_NOMOVE);
}

void CSkinSliderCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	SetCapture();

	m_bLButtonDown = TRUE;

	if( m_bVertical ) {

		if( abs( point.y - Pos2Pixel( m_nPos ) ) <= (int)m_ImageThumb.GetHeight() / 2 )
			m_nMouseOffset = point.y - Pos2Pixel( m_nPos );
		else
			m_nMouseOffset = 0;

	} else {

		if( abs( point.x - Pos2Pixel( m_nPos ) ) <= (int)m_ImageThumb.GetWidth() / 2 )
			m_nMouseOffset = point.x - Pos2Pixel( m_nPos );
		else
			m_nMouseOffset = 0;
	}

	OnMouseMove( nFlags, point );
	Invalidate(FALSE);

	CSliderCtrl::OnLButtonDown(nFlags, point);
}

void CSkinSliderCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
	if( !m_bLButtonDown )
		return;

	int nPixel;
	CRect rcClient;
	GetClientRect(&rcClient);

	if( m_bVertical ) {

		nPixel = point.y - m_nMouseOffset;

		if( nPixel > rcClient.Height() -(int)m_ImageThumb.GetHeight()/2 )
			nPixel = rcClient.Height() - (int)m_ImageThumb.GetHeight()/2;

		if( nPixel < rcClient.Height()/2 )
			nPixel = rcClient.Height()/2;

	} else {

		nPixel = point.x - m_nMouseOffset;

		if( nPixel < (int)m_ImageThumb.GetWidth()/2 )
			nPixel = m_ImageThumb.GetWidth()/2;

		if( nPixel > rcClient.Width() - (int)m_ImageThumb.GetWidth()/2 )
			nPixel = rcClient.Width() - (int)m_ImageThumb.GetWidth()/2;
	}

	if( Pos2Pixel(m_nPos) != nPixel ) {

		SetPos( Pixel2Pos( nPixel ) );

		::PostMessage(
			GetParent()->GetSafeHwnd(), WM_SKINSLIDER_MOVING,
			GetDlgCtrlID(), m_nPos );
	}

	CSliderCtrl::OnMouseMove(nFlags, point);
}

void CSkinSliderCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
	ReleaseCapture();
	m_bLButtonDown = FALSE;

	Invalidate(FALSE);

	::PostMessage(
		GetParent()->GetSafeHwnd(),	WM_SKINSLIDER_MOVED,
		GetDlgCtrlID(), m_nPos );

	CSliderCtrl::OnLButtonUp(nFlags, point);
}

UINT CSkinSliderCtrl::OnGetDlgCode() 
{
	if( GetKeyState(VK_TAB) >= 0 ) {

		return  DLGC_WANTALLKEYS;
	}

	return CSliderCtrl::OnGetDlgCode();
}

void CSkinSliderCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	switch( nChar ) 
	{
	case VK_LEFT :
	case VK_UP :
		SetPos( m_nPos-1 );
		break;
	case VK_RIGHT :
	case VK_DOWN :

		SetPos( m_nPos+1 );
		break;
	case VK_HOME :
		SetPos( m_nMin );
		break;
	case VK_END :
		SetPos( m_nMax );
		break;
	case VK_PRIOR :
		SetPos( m_nPos - m_nPage );
		break;
	case VK_NEXT :
		SetPos( m_nPos + m_nPage );
		break;
	default :

		CSliderCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
		return;
	}

	::PostMessage(
		GetParent()->GetSafeHwnd(),	WM_SKINSLIDER_MOVED,
		GetDlgCtrlID(), m_nPos );

	CSliderCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CSkinSliderCtrl::OnDestroy() 
{
	CSliderCtrl::OnDestroy();
}

HBRUSH CSkinSliderCtrl::CtlColor(CDC* pDC, UINT nCtlColor)
{
	pDC->SetBkMode(TRANSPARENT);
	return (HBRUSH)GetStockObject(NULL_BRUSH);
}


//////////////////////////////////////////////////////////////////////////